package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.modules.dto.CConditionDTO;
import com.key.modules.dto.ComplainDTO;
import com.key.modules.dto.FComplainDTO;
import com.key.modules.dto.HComplainDTO;
import com.key.modules.entity.Complain;
import com.key.modules.entity.ICustomers;
import com.key.modules.mapper.ComplainMapper;
import com.key.modules.service.ComplainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ComplainServiceImpl implements ComplainService {

    @Autowired
    private ComplainMapper complainMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public RespResult addComplain(ComplainDTO complainDTO) {
        // log.info("{}==添加客户投诉", SecurityUtils.getSubject().getPrincipal());
        Long c_no = redisTemplate.opsForValue().increment("c_no");
        Complain complain=new Complain();
        BeanUtil.copyProperties(complainDTO,complain);
        complain.setCNo(String.format("%s%05d", Constants.CPRE, c_no));
        complain.setCreateTime(new Date());
        int i = complainMapper.insert(complain);
        if (i>0){
            log.info("{}==客户投诉添加成功",complain.toString());
            return RespResult.success();
        }
        return RespResult.error("添加失败");
    }

    @Override
    public RespResult findAllComplain(CConditionDTO cConditionDTO) {
        Page<Complain> page=new Page<>(cConditionDTO.getPageNum(),cConditionDTO.getPageSize());
        LambdaQueryWrapper<Complain> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(cConditionDTO.getUserName()),Complain::getUserName,cConditionDTO.getUserName())
                .eq(StrUtil.isNotBlank(cConditionDTO.getWNo()),Complain::getWNo,cConditionDTO.getWNo())
                .eq(cConditionDTO.getCType()!=null,Complain::getCType,cConditionDTO.getCType())
                .ge(cConditionDTO.getStartTime()!=null,Complain::getCreateTime,cConditionDTO.getStartTime())
                .le(cConditionDTO.getEndTime()!=null,Complain::getCreateTime,cConditionDTO.getEndTime())
                .orderBy(cConditionDTO.getOrder()!=null,cConditionDTO.getOrder()==Constants.ASC,Complain::getCreateTime);
        Page<Complain> complainPage = complainMapper.selectPage(page,lambdaQueryWrapper);
        System.out.println("当前页码数："+complainPage.getCurrent());
        System.out.println("每页显示条数："+complainPage.getSize());
        System.out.println("总条数："+complainPage.getTotal());
        System.out.println("总页数："+complainPage.getPages());
        System.out.println("查询数据："+complainPage.getRecords());
        return RespResult.success(complainPage);
    }

    @Override
    public RespResult findComplainById(Integer id) {
        Complain complain = complainMapper.selectById(id);
        if (complain!=null){
            return RespResult.success(complain);
        }
        return RespResult.error("查询失败");
    }

    @Override
    public RespResult updateComplain(ComplainDTO complainDTO) {
        // log.info("{}==修改客户投诉", SecurityUtils.getSubject().getPrincipal());
        Complain complain=new Complain();
        BeanUtil.copyProperties(complainDTO,complain);
        int i = complainMapper.updateById(complain);
        if (i>0){
            log.info("{}==修改成功", complain.toString());
            return RespResult.success();
        }
        return RespResult.error("修改失败");
    }

    @Override
    public RespResult deleteComplain(List<Integer> ids) {
        // log.info("{}==删除客户投诉", SecurityUtils.getSubject().getPrincipal());
        try {
            complainMapper.deleteBatchIds(ids);
            log.info("{}==删除成功",ids);
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("删除失败");
        }
    }

    @Override
    public RespResult updateHState(HComplainDTO hComplainDTO) {
        Complain complain=new Complain();
        BeanUtil.copyProperties(hComplainDTO,complain);
        complain.setState(Constants.CSTATE);
        int i = complainMapper.updateById(complain);
        if (i>0){
            log.info("{}==处理成功", complain.toString());
            return RespResult.success();
        }
        return RespResult.error("处理失败");
    }

    @Override
    public RespResult updateFState(FComplainDTO fComplainDTO) {
        Complain complain=new Complain();
        BeanUtil.copyProperties(fComplainDTO,complain);
        complain.setState(Constants.FSTATE);
        int i = complainMapper.updateById(complain);
        if (i>0){
            log.info("{}==完结成功", complain.toString());
            return RespResult.success();
        }
        return RespResult.error("完结失败");

    }


}
